package edu.gdut.tiantian.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import edu.gdut.tiantian.entity.Category;

@Repository
public interface CategoryDao {
	//添加类别
	@Insert("insert into category(categoryName,categoryDesc) values(#{categoryName},#{categoryDesc})")
	void saveCategory(Category category);
	
	//删除类别
	@Delete("delete from category where categoryId=#{categoryId}")
	void delCategory(Integer categoryId);
	
	//更新类别
	@Update("update category set categoryName=#{categoryName},categoryDesc=#{categoryDesc} where categoryId=#{categoryId}")
	void updateCategory(Category category);
	
	//获取类别详情
	@Select("select * from category where categoryId=#{categoryId}")
	Category findById(Integer categoryId);

	//条件查询
	@Select("<script> select * from category where 1=1 "
			+ "<foreach collection='condition' index='keys' item='values'>"
			+ "<if test='keys == " +"\"categoryName\" and keys != null" +"'>"
			+ " and categoryName like CONCAT('%',#{values },'%')"
			+ "</if>"
			+ "<if test='keys == " +"\"categoryId\" and keys != null" +"'>"
			+ " and categoryId=#{values}"
			+ "</if>"
			+ "</foreach>"
			+ "</script>")
	List<Category> findByCondition(@Param("condition")Map<String,String> condition);
	
	@Select("select * from category")
	List<Category> findAll();
}
